<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 输入、处理、输出
        // indexOf()方法返回再数组中可以找到一个给定元素的第一个索引，如果不存在，则返回-1。
        let arr = ['ant', 'bison', 'camel', 'duck', 'bison', 'camel'];
        // console.log(arr.indexOf('camel'));
        // console.log(arr.indexOf("camel1"));

        // 自己封装
        function indexOf(arr, ele) {
            var index = -1;
            if (!(arr instanceof Array)) {
                return -1;
            }
            for (var i = 0; i < arr.length; i++) {
                if (ele == arr[i]) {
                    index = i;
                    break;
                }
            }
            return index;
        }
        console.log(indexOf(arr, "camel"));

        function indexOf(arr, ele) {
            var index = -1;
            if (!Array.isArray(arr)) {
                return -1;
            }
            for (var i = arr.length-1; i >=0; i--) {
                if (ele == arr[i]) {
                    index = i;
                    break;
                }
            }
            return index;
        }
        console.log(indexOf(arr, "bison"));
    </script>
</body>

</html>